package cn.com.nes.site.service.project.impl;

import cn.com.nes.common.exception.IllegalBusinessException;
import cn.com.nes.mybatis.agent.project.entity.ProjectLog;
import cn.com.nes.mybatis.agent.project.entity.TProjectScheduling;
import cn.com.nes.mybatis.agent.project.mapper.TProjectSchedulingMapper;
import cn.com.nes.site.entity.bo.project.ProjectSchedulingSaveBO;
import cn.com.nes.site.service.project.ProjectLogService;
import cn.com.nes.site.service.project.ProjectSchedulingService;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 项目排期业务实现
 */
@Slf4j
@Service
public class ProjectSchedulingServiceImpl extends ServiceImpl<TProjectSchedulingMapper, TProjectScheduling> implements ProjectSchedulingService {


    @Resource
    ProjectLogService projectLogService;

    /**
     * 查询项目排期列表
     * @param projectUuids 项目uuid 复数
     * @return
     */
    @Override
    public List<TProjectScheduling> getList(List<String> projectUuids) throws Exception {
        return this.baseMapper.selectList(new QueryWrapper<TProjectScheduling>().in("project_uuid", projectUuids));
    }

    /**
     * 查询项目排期
     * @param projectUuid 项目uuid
     * @return
     */
    @Override
    public TProjectScheduling getOne(String projectUuid) throws Exception {
        TProjectScheduling query = new TProjectScheduling();
        query.setProjectUuid(projectUuid);
        TProjectScheduling tProjectScheduling = this.baseMapper.selectOne(new QueryWrapper<>(query));
        // if (ObjectUtil.isNull(tProjectScheduling)) {
        //     throw new BusinessException("项目排期不存在！");
        // }
        return tProjectScheduling;
    }

    /**
     * 查询项目排期
     * @param id 主键ID
     * @return
     */
    @Override
    public TProjectScheduling getOne(Long id) throws Exception {
        TProjectScheduling tProjectScheduling = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(tProjectScheduling)) {
            throw new IllegalBusinessException("项目排期不存在！");
        }
        return tProjectScheduling;
    }

    /**
     * List 转 Map
     * 格式： <projectUuid, TProjectScheduling>
     * @param schedulingList
     * @return
     */
    @Override
    public Map<String, TProjectScheduling> convertToMapOfKeyIsProjectUuid(List<TProjectScheduling> schedulingList) {
        if (schedulingList.isEmpty()) {
            return MapUtil.empty();
        }
        return schedulingList.stream()
                .collect(Collectors.toMap(TProjectScheduling::getProjectUuid, scheduling -> scheduling));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TProjectScheduling saveProjectScheduling(ProjectSchedulingSaveBO bo) throws Exception {
        TProjectScheduling projectScheduling = this.getOne(new QueryWrapper<TProjectScheduling>().eq("project_uuid", bo.getProjectUuid()));
        if (ObjectUtil.isNull(projectScheduling)) {
            projectScheduling = new TProjectScheduling();
            projectScheduling.setUuid(UUID.randomUUID().toString());
            projectScheduling.setProjectUuid(bo.getProjectUuid());
            projectScheduling.setCreateuid(bo.getOperateUserId());
            projectScheduling.setCreatetime(bo.getOperateDate());

            // 记录项目操作记录-新增排期
            ProjectLog projectLog = new ProjectLog();
            projectLog.setLinkId(bo.getProjectUuid());
            projectLog.setLinkType("1");
            projectLog.setTitle("项目排期");
            projectLog.setRemark("新增排期： " + bo.getServiceStartTime() + "-" + bo.getServiceEndTime());
            projectLog.setCreateuid(bo.getOperateUserId());
            projectLog.setCreatetime(bo.getOperateDate());
            projectLogService.save(projectLog);
        }
        projectScheduling.setDepartmentId(bo.getDepartmentId());
        projectScheduling.setStartTime(bo.getServiceStartTime());
        projectScheduling.setEndTime(bo.getServiceEndTime());
        projectScheduling.setModifyuid(bo.getOperateUserId());
        projectScheduling.setModifytime(bo.getOperateDate());

        // 记录项目操作记录-修改排期
        TProjectScheduling query = new TProjectScheduling();
        query.setProjectUuid(bo.getProjectUuid());
        query.setStartTime(bo.getServiceStartTime());
        query.setEndTime(bo.getServiceEndTime());
        TProjectScheduling edit = this.baseMapper.selectOne(new QueryWrapper<>(query));
        if (ObjectUtil.isNull(edit)) {
            ProjectLog projectLog = new ProjectLog();
            projectLog.setLinkId(bo.getProjectUuid());
            projectLog.setLinkType("1");
            projectLog.setTitle("项目排期");
            projectLog.setRemark("修改排期： " + bo.getServiceStartTime() + "-" + bo.getServiceEndTime());
            projectLog.setCreateuid(bo.getOperateUserId());
            projectLog.setCreatetime(bo.getOperateDate());
            projectLogService.save(projectLog);
        }

        this.saveOrUpdate(projectScheduling);

        return projectScheduling;
    }


}
